/**
 * Created by Administrator on 2017/5/1.
 */

!(function($){
    var phone = {
        userId: $.fn.cookie('userId'),
        isLogin: $.fn.cookie('isLogin'),
        token : $.fn.cookie('token'),
        params:{},
        init:function(){
            this.submit();
            this.getSmsCode();
        },
        checkMobile: function(str) {
            var re =/^1[3|7|5|8]\d{9}$/;
            if (re.test(str)) {
                return true;
            } else {
                return false;
            }
        },
        getSmsCode:function(){
            var self = this;
            var flag = false;
            $('.weui-vcode-btn').off('click').on('click',function(){
                var _this = $(this);
                flag = false;
                var countdown = 60;
                var phoneNum = $('#phonenum').val().trim();
                if(self.checkMobile(phoneNum) && flag==false){
                    _this.addClass('disabled');
                    _this.attr('disabled',true);
                    var timer = setInterval(function(){
                        countdown--;
                        _this.html(countdown + "秒后重新获取");
                        if (countdown == 0) {
                            flag = true;
                            clearInterval(timer);//停止计时器
                            _this.attr("disabled",false);//启用按钮
                            _this.removeClass('disabled');
                            _this.html("重新发送");
                        }
                    }, 1000);
                    $.ajax({
                        url:API_URL+SMS_CODE_URL,
                        type:'post',
                        dataType:"jsonp",
                        data:{phoneNum:phoneNum},
                        success:function(data){
                            if(data.ok==true){
                                $.toptips('验证码已发送，请在手机端查收！','ok');
                                flag = true;
                                self.params.smsKey = data.result.smsKey;
                                self.params.phoneNum = data.result.phoneNum;
                                self.params.token = self.token;
                                self.params.userId = self.userId;
                            }else{
                                $.toptips(data.msg);
                                return;
                            }
                        },
                        error:function(data){
                            $.toptips(data.msg);
                        }
                    });
                }else{
                    $.toptips('请输入手机号！','info');
                }
            })

        },
        submit:function(){
            var self = this;
            var $form = $("#form");
            $form.form();
            $("#formSubmitBtn").on("click", function(){
                self.params.smsCode = $('#smsCode').val();
                $form.validate(function(error){
                    if(error){
                        $.toptips(error.msg);
                    }else{
                        $.ajax({
                            url:API_URL+BIND_PHONE_URL,
                            type:'post',
                            dataType:'jsonp',
                            data:self.params,
                            success:function(data){
                                if(data.ok==true){
                                    $.alert('绑定成功!');
                                    setTimeout(function(){
                                        window.location.href="set.html";
                                    },1000)
                                }else{
                                    $.toptips("验证码错误!");
                                }
                            },
                            error:function(data){
                                if(data){
                                    $.toptips(data.msg);
                                }
                            }
                        })

                    }
                });

            });
        }
    };
    $(function(){
        phone.init();
    })
}(Zepto));

